package com.hhck.controller;

import com.hhck.entity.ProductType;
import com.hhck.entity.Result;
import com.hhck.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/productCategory")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 查询商品类型树
     */
    @GetMapping("/product-category-tree")
    public Result productCategoryTree() {
        return Result.ok(productTypeService.queryAllProductType());
    }

    /**
     * 校验分类编码是否存在
     */
    @GetMapping("/verify-type-code")
    public Result verifyTypeCode(ProductType productType) {
        return productTypeService.queryProductByTypeCode(productType);
    }

    /**
     * 添加商品分类
     */
    @PostMapping("/type-add")
    public Result typeAdd(@RequestBody ProductType productType) {
        return productTypeService.saveProductType(productType);
    }

    /**
     * 删除商品分类及其子类
     */
    @DeleteMapping("/type-delete/{typeId}")
    public Result typeDelete(@PathVariable("typeId") Integer typeId) {
        return productTypeService.removeProductTypeAndChildType(typeId);
    }

    /**
     * 修改商品分类
     */
    @PutMapping("/type-update")
    public Result typeUpdate(@RequestBody ProductType productType) {
        return productTypeService.modifyProductTypeByTypeId(productType);
    }

}

